//@ts-nocheck
import {createStore} from 'redux';
import rootReducer from './reducers';

//如何定一个专业的中间件 ---> 实现一个日志中间件
//格式是定死的 柯里化 三层
const logger1 = function (api: any) {//api 现在你理解为接受的是store即可
  return function (next: any) {//调用下一个中间件的方法 现在你可以理解为store.dispatch
    return function (action: any) {//dispatch要派发的动作
      console.log('变更前的老状态1', api.getState());//store.dispatch
      next(action);//调用下一个中间件
      console.log('变更后的新状态1', api.getState());
    }
    //↑以后再调用store.dispatch调用就是这个函数, applyMiddleware里会换掉原本的dispatch(return {...store, dispatch})
  }
}

function applyMiddleware(middleware){
  return function(createStore){
    return function(reducer){
      const store = createStore(reducer);
      const dispatch = middleware(store)(store.dispatch);
      return {
        ...store,
        dispatch
      }
    }
  }
}

//const store = createStore(rootReducer);
const store = applyMiddleware(logger1)(createStore)(rootReducer);

export default store;
